/**
 * ReportGear(2011)
 */
package com.reportgear.report.model.calculator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.reportgear.core.data.SalaryHelper;
import com.reportgear.core.data.model.SalaryDataObject;
import com.reportgear.report.model.ReportModel;
import com.reportgear.report.model.cell.Cell;
import com.reportgear.report.model.cell.value.CrossDetailDataObject;
import com.reportgear.report.model.cell.value.support.CorssDataValueObject;

/**
 * 
 * 交叉数据明细对象值计算
 * 
 * @version 1.0 2011-4-10
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class CrossDetailDataObjectValueCalculator extends AbstractCalculator {
	public CrossDetailDataObjectValueCalculator() {

	}

	@Override
	public ReportModel calculate(ReportModel model, CalcParameter parameter) {
		if (logger.isDebugEnabled()) {
			logger.debug("计算模型：电信薪酬对象值计算");
		}

		ReportModel calcModel = model;

		Set<Integer> colSet = new HashSet<Integer>();
		Iterator<Cell> cells1 = calcModel.getCellsIteratorByCellElementClass(CrossDetailDataObject.class);
		for (Iterator<Cell> i = cells1; i.hasNext();) {
			Cell cell = i.next();

			CrossDetailDataObject value = (CrossDetailDataObject) cell.getValue();
			colSet.addAll(value.getxIds());
		}

		List<SalaryDataObject> dataList = SalaryHelper.INSTANCE.getPayDetail(parameter.getPaymentId(), parameter
				.getSalaryObjectType(), parameter.getMaxLevel(), parameter.getSelectObjSet(), colSet);
		List<CorssDataValueObject> data = new ArrayList<CorssDataValueObject>();
		for (SalaryDataObject sdo : dataList) {
			data.add(new CorssDataValueObject(sdo.getInt("itemID"), sdo.getInt("objectID"), sdo.getDouble("payValue")));
		}

		Iterator<Cell> cells = calcModel.getCellsIteratorByCellElementClass(CrossDetailDataObject.class);
		for (Iterator<Cell> i = cells; i.hasNext();) {
			Cell cell = i.next();

			CrossDetailDataObject value = (CrossDetailDataObject) cell.getValue();
			// 对象内部过滤，自己取舍
			value.addValues(data);
		}

		return calcModel;
	}
}
